// ==UserScript==
// @name         全国企业信息插件
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  获取企业信息
// @author       zzq
// @include      *
// @icon         https://www.google.com/s2/favicons?sz=64&domain=kingdee.com
// @grant        GM_xmlhttpRequest
// @grant        GM_registerMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_info
// @grant        unsafeWindow
// @grant        GM_setClipboard
// @connect      cdn.jsdelivr.net
// @connect      app.gsxt.gov.cn

// @require      https://cdn.jsdelivr.net/npm/hotkeys-js@3.7.2/dist/hotkeys.min.js




// ==/UserScript==
//加载layui
let script = document.createElement('link');
script.setAttribute('rel', 'stylesheet');
script.setAttribute('type', 'text/css');
script.href = "https://unpkg.com/layui@2.6.8/dist/css/layui.css";
document.documentElement.appendChild(script);

let scriptjs = document.createElement('script');
scriptjs.setAttribute('type', 'text/javascript');
scriptjs.src = "https://www.layuicdn.com/layui-v2.6.8/layui.js";
document.documentElement.appendChild(scriptjs);

//注册热键
(function () {
    'use strict';

    // Your code here...
    hotkeys('alt+c', function () {
        start()
    });
    hotkeys('esc', function () {
        console.log(clipdata.getData('text/plain'));
        layer.closeAll();
    });
})();


//热键启动
function start() {

    var tempTxt
    if (unsafeWindow.getSelection) {
        tempTxt = unsafeWindow.getSelection();
    } else if (document.selection) {
        tempTxt = document.selection.createRange();
    }
    GM_setClipboard(tempTxt.toString());
    send_post(tempTxt.toString())
}

//列表查询
function send_post(supplierName) {
    supplierName = supplierName.replace(/^\s*|\s*$/g,"")
    let postUrl = "http://app.gsxt.gov.cn/gsxt/cn/gov/saic/web/controller/PrimaryInfoIndexAppController/search?page=1"
    let sendData = {
        "searchword": supplierName,
        "conditions": {
            "excep_tab": "0",
            "ill_tab": "0",
            "area": "0",
            "cStatus": "0",
            "xzxk": "0",
            "xzcf": "0",
            "dydj": "0"
        },
        "sourceType": "A"
    }
    let prepareData;
    let title;
    GM_xmlhttpRequest({
        method: "POST",
        url: postUrl,
        headers: {
            "Content-Type": "application/json;charset=utf-8",
            "user-agent": "Mozilla/5.0 (Linux; Android 10; YAL-AL00 Build/HUAWEIYAL-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/83.0.4103.106 Mobile Safari/537.36 Html5Plus/1.0",
            "x-requested-with": "XMLHttpRequest"
        },
        data: JSON.stringify(sendData),
        onload: function (response) {
            if (response.status === 200) {
                //请求成功
                let result_list = JSON.parse(response.response)
                if (result_list.data.result && result_list.data.result.data) {
                    const dataList = result_list.data.result.data
                    let filterData = dataList.filter(item => item.entName.replace("<font color=red>", "").replace("</font>", "") === supplierName)
                    if (filterData && filterData.length > 0) {
                        prepareData = filterData[0]
                        getSupplierInfo(prepareData.pripid, prepareData.nodeNum, prepareData.entType)

                    } else {
                        layer.msg('供应商信息异常或接口异常，请稍后重试');
                    }
                } else {
                    layer.msg('供应商信息异常或接口异常，请稍后重试');
                }
            }
        }
    });

}
//获取详情
function getSupplierInfo(pripid, nodeNum, entType) {
    let infoUrl = `http://app.gsxt.gov.cn/gsxt/corp-query-entprise-info-primaryinfoapp-entbaseInfo-${pripid}.html?nodeNum=${nodeNum}&entType=${entType}&sourceType=A`
    GM_xmlhttpRequest({
        method: "POST",
        url: infoUrl,
        headers: {
            "Content-Type": "application/json;charset=utf-8",
            "user-agent": "Mozilla/5.0 (Linux; Android 10; YAL-AL00 Build/HUAWEIYAL-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/83.0.4103.106 Mobile Safari/537.36 Html5Plus/1.0",
            "x-requested-with": "XMLHttpRequest"
        },

        onload: function (response) {
            layer.open({
                area: ['30%', '300px'],
                type: 1,
                shade: false,
                title: false,
                content: showPanels(JSON.parse(response.response).result),
                offset: 'rt',
                cancel: function () {

                }
            });

        }
    })
}

//结果显示
function showPanels(info) {
console.log(info)
    info.entName = info.entName === undefined  ?  info.traName : info.entName
    info.dom = info.dom === undefined  ?  info.opLoc : info.dom

    let panleCard = `    <div class="layui-card" style="background-color:rgb(53,149,204);color:	#F8F8FF">
        <div class="layui-card-header" style="color:#F8F8FF">名称:${info.entName}</div>
        <div class="layui-card-body">
            <div class="layui-row layui-col-space10">
                <div class="layui-col-md12">
                   <span style="color:#a5c2eb">纳税人识别号:</span> <span>${info.uniscId}</span> &nbsp;<button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger  layui-btn-xs copyText"><i class="layui-icon layui-icon-template"></i></button>
                </div>
            </div>
            <div class="layui-row layui-col-space10">
                <div class="layui-col-md6">
                    <span style="color:#a5c2eb">法人代表：</span><span>${info.name}</span>&nbsp;<button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger  layui-btn-xs copyText"><i class="layui-icon layui-icon-template"></i></button>
                </div>
                <div class="layui-col-md6">
                    <span style="color:#a5c2eb">成立时间：</span><span>${info.opFrom}</span>&nbsp;<button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger  layui-btn-xs copyText"><i class="layui-icon layui-icon-template"></i></button>
                </div>
            </div>
            <div class="layui-row layui-col-space3">
                <div class="layui-col-md6">
                    <span style="color:#a5c2eb">营业状态:</span><span>${info.regState_CN}</span>&nbsp;<button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger  layui-btn-xs copyText"><i class="layui-icon layui-icon-template"></i></button>
                </div>
                <div class="layui-col-md6">
                    <span style="color:#a5c2eb">注册资金：</span><span>${info.regCap}</span>&nbsp;(万元)&nbsp;<button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger  layui-btn-xs copyText"><i class="layui-icon layui-icon-template"></i></button>
                </div>

            </div>
            <div class="layui-row layui-col-space10">
                <div class="layui-col-md12">
                   <span style="color:#a5c2eb"> 地址：</span> <span>${info.dom}</span>&nbsp;<button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger  layui-btn-xs copyText"><i class="layui-icon layui-icon-template"></i></button>
                </div>
            </div>
            <div class="layui-row layui-col-space10">
                <div class="layui-col-md12">
                    <span style="color:#a5c2eb"> 经营范围:</span> <span>${info.opScope}</span>&nbsp;<button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger  layui-btn-xs copyText"><i class="layui-icon layui-icon-template"></i></button>
                </div>
            </div>
        </div>
    </div>`
    return panleCard
}

//复制事件
$(document).on('click', ".copyText", function () {
    GM_setClipboard($(this).prev().text(), 'text')
    layer.msg('复制成功');
});